package com.wswy.wzcx.ui.njdb

import android.text.format.DateFormat
import com.wswy.wzcx.model.car.CarStatus
import com.wswy.wzcx.model.license.UserCarInfo
import timber.log.Timber
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.TimeUnit
import kotlin.math.abs

class NjVoModel(val userCarInfo: UserCarInfo) {

    companion object {
        private val sdf = SimpleDateFormat("yyyy-MM-dd")
        /**
         * 预约开始月份
         */
        const val TAG_MSG_YY = "tag_msg_yy"
        const val TAG_MSG_BRFOE_6 = "tag_msg_before_6" //提前6个月
        const val TAG_MSG_BRFOE_3 = "tag_msg_before_3" //提前3个月
        const val TAG_MSG_BRFOE_1 = "tag_msg_before_1" //提前1个月
    }


    /**
     * 状态
     */
    val status by lazy {
        getInspectionStatus2()
    }

    var days: Int = 0
        private set


    private fun getInspectionStatus2(): Int {
        var retStatus = CarStatus.NJ_NONE
        userCarInfo.carExtendInfo?.inspection?.runCatching {
            days = remainDay
            when(status){
                CarStatus.NORMAL -> {
                    //未到期
                    retStatus = CarStatus.NJ_NORMAL
                }
                CarStatus.PROMPT -> {
                    //可办理
                    retStatus = CarStatus.NJ_CAN_APPLY
                }
                CarStatus.OVERDUE -> {
                    //已逾期
                    retStatus = CarStatus.NJ_OVERDUE
                }
            }

        }
        return retStatus
    }

    private fun getInspectionStatus(): Int {
        var retStatus = CarStatus.NJ_NONE
        userCarInfo.carExtendInfo?.inspection?.runCatching {
            val date = sdf.parse(validityDate)
            val calendar = Calendar.getInstance()
            calendar.time = date

            //当月最大天数
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DATE))

            //最后到期时间
            //12 小时 60*60*12*1000
            val endTime = calendar.timeInMillis

            //向前回滚2个月，并设置为1号
            calendar.add(Calendar.MONTH, -2)
            calendar.set(Calendar.DAY_OF_MONTH, 1)

            //开始时间
            val startTime = calendar.timeInMillis

//            //最小预约时间
//            calendar.add(Calendar.MONTH,-6)
//
//            val minYTime = calendar.timeInMillis

            val current = System.currentTimeMillis()

            days = remainDay

            Timber.e(" ${userCarInfo.carNo}  startTime:$startTime  ${DateFormat.format("yyyy-MM-dd", startTime)}   endTime:$endTime  ${DateFormat.format("yyyy-MM-dd", endTime)}  ")

//            if (current > endTime) {
//                //已逾期
//                retStatus = CarStatus.NJ_OVERDUE
//                days = abs(remainDay)
//
//            } else if (current < minYTime){
//                //无需办理
//                retStatus = CarStatus.NJ_NORMAL
//
//            } else if(current in (startTime + 1) until endTime) {
//                //可办理
//                retStatus = CarStatus.NJ_CAN_APPLY
//
//            } else if (current in (minYTime + 1) until startTime) {
//                //可预约
//                retStatus = CarStatus.NJ_CAN_RESERVATION
//
//            }


            if (current > endTime) {
                //已逾期
                retStatus = CarStatus.NJ_OVERDUE
                days = abs(remainDay)


            } else if (current in (startTime + 1) until endTime) {
                //可办理
                retStatus = CarStatus.NJ_CAN_APPLY

                days = TimeUnit.DAYS.convert(endTime - current, TimeUnit.MILLISECONDS).toInt()

            } else if (current < startTime) {
                //无需办理
                retStatus = CarStatus.NJ_NORMAL
                days = TimeUnit.DAYS.convert(startTime - current, TimeUnit.MILLISECONDS).toInt()
            }

        }

        return retStatus
    }

    override fun toString(): String {
        return "NjVoModel(userCarInfo=$userCarInfo,status=$status, days=$days)"
    }


}